package com.cloudweb.oa.service.impl;

import cn.js.fan.util.ErrMsgException;
import cn.js.fan.util.ParamUtil;
import cn.js.fan.util.StrUtil;
import com.cloudweb.oa.api.ILicense;
import com.cloudweb.oa.api.IWorkflowProService;
import com.cloudweb.oa.cache.UserCache;
import com.cloudweb.oa.entity.User;
import com.cloudweb.oa.security.AuthUtil;
import com.cloudweb.oa.utils.SpringUtil;
import com.cloudwebsoft.framework.util.LogUtil;
import com.redmoon.oa.Config;
import com.redmoon.oa.flow.*;
import com.redmoon.oa.person.UserDb;
import com.redmoon.oa.pvg.Privilege;
import com.redmoon.oa.sys.DebugUtil;
import com.redmoon.oa.ui.LocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Vector;

@Service
public class WorkflowProServiceImpl implements IWorkflowProService {

    @Autowired
    AuthUtil authUtil;

    @Autowired
    HttpServletRequest request;

    /**
     * 加签
     * 加签MyActionDb中的privMyActionId记录的始终为节点上的原始处理人员
     *
     * @param request HttpServletRequest
     * @return boolean
     * @throws ErrMsgException
     */
    @Override
    public boolean addPlus(HttpServletRequest request, long myActionId, int type, int mode) throws ErrMsgException {
        // int actionId = ParamUtil.getInt(request, "actionId");
        // String users = request.getParameter("users"); // 解决ajax中文问题
        String users = ParamUtil.get(request, "users");

        Privilege pvg = new Privilege();
        String curUser = pvg.getUser(request);
        String[] ary = StrUtil.split(users, ",");
        if (ary == null) {
            throw new ErrMsgException("请选择人员");
        }

        users = "";
        for (String user : ary) {
            if (user.equals(curUser)) {
                continue;
            }
            users += (users.equals("") ? "" : ",") + user;
        }
        if ("".equals(users)) {
            throw new ErrMsgException("请选择加签人员！");
        }

        String cwsWorkflowResult = ParamUtil.get(request, "cwsWorkflowResult");
        cn.js.fan.mail.SendMail sendmail = WorkflowDb.getSendMail();
        MyActionDb mad = new MyActionDb();
        mad = mad.getMyActionDb(myActionId);
        WorkflowActionDb wa = new WorkflowActionDb();
        wa = wa.getWorkflowActionDb((int) mad.getActionId());
        com.alibaba.fastjson.JSONObject json = new com.alibaba.fastjson.JSONObject();
        json.put("type", type);
        json.put("mode", mode);
        json.put("users", users);
        json.put("from", curUser);
        json.put("internal", wa.getInternalName());
        wa.setPlus(json.toString());
        boolean re = wa.saveOnlyToDb();
        if (re) {
            ary = StrUtil.split(users, ",");
            if (ary != null) {
                if (!"".equals(cwsWorkflowResult)) {
                    mad.setResult(cwsWorkflowResult);
                    mad.save();
                }

                WorkflowDb wf = new WorkflowDb();
                wf = wf.getWorkflowDb((int) mad.getFlowId());

                long privMyActionId = mad.getPrivMyActionId();
                MyActionDb privMyActionDb = mad.getMyActionDb(privMyActionId);
                WorkflowActionDb privAction = wa.getWorkflowActionDb((int) privMyActionDb.getActionId());

                // 前加签
                if (type == WorkflowActionDb.PLUS_TYPE_BEFORE) {
                    // 如果为前加签，则本myActionId应该被置为已完成
                    wa.changeMyActionDb(mad, pvg.getUser(request));
                    // 如果为顺序加签，则先发给第一个人待办通知
                    if (mode == WorkflowActionDb.PLUS_MODE_ORDER) {
                        MyActionDb plusmad = wf.notifyUser(ary[0], new java.util.Date(), mad.getId(), privAction, wa,
                                WorkflowActionDb.STATE_PLUS, wa.getFlowId());
                        wf.sendNotifyMsgAndEmail(request, plusmad, sendmail);
                    } else { // 否则发给全部加签人员
                        for (String userName : ary) {
                            MyActionDb plusmad = wf.notifyUser(userName, new java.util.Date(), mad.getId(), privAction, wa,
                                    WorkflowActionDb.STATE_PLUS, wa.getFlowId());
                            wf.sendNotifyMsgAndEmail(request, plusmad, sendmail);
                        }
                    }
                } else if (type == WorkflowActionDb.PLUS_TYPE_CONCURRENT) { // 并签，给每个加签人员发待办通知
                    for (String userName : ary) {
                        MyActionDb plusmad = wf.notifyUser(userName, new java.util.Date(), mad.getId(), privAction, wa,
                                WorkflowActionDb.STATE_PLUS, wa.getFlowId());
                        wf.sendNotifyMsgAndEmail(request, plusmad, sendmail);
                    }
                }
            }
        }
        return re;
    }

}
